from openai import OpenAI

def create_client(api_key):
    """创建OpenAI客户端，配置为使用Deepseek API"""
    return OpenAI(
        api_key=api_key,
        base_url='https://api.deepseek.com/v1'
    )

def chat_with_deepseek(client, message, model="deepseek-chat"):
    """使用Deepseek API进行对话"""
    try:
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "user", "content": message}
            ]
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

def main():
    # 从环境变量或配置文件中获取API密钥
    api_key = input("请输入你的Deepseek API密钥: ")
    
    # 创建客户端
    client = create_client(api_key)
    
    print("欢迎使用Deepseek API示例程序！输入'quit'退出。")
    
    while True:
        user_input = input("\n请输入你的问题: ")
        if user_input.lower() == 'quit':
            break
            
        response = chat_with_deepseek(client, user_input)
        print(f"\nDeepseek的回答: {response}")

if __name__ == "__main__":
    main()